#!/usr/bin/env python

# console.example
#
# Copyright (C) 2010 Tactical Network Solutions
# Author: Benjamin Bengfort <bbengfort@tacnetsol.com>
# URL: <http://tacnetsol.com>
#
# $Id: example.py 2 2010-11-29 13:48:00 -0400 bbengfort $

"""
Example of using the console framework.

Overview
========
This code represents a standard methodology for programming console programs
using the L{console} framework. Essentially, external functionality is imported
from other modules, and commands are entry points to that functionality. Extend
the L{InteractiveShell} and add custom commands either as internal private
classes, or as their own class hierarchy, and then add them during
L{CustomShell.__init__}.

Good luck, and good coding!

Class Hierarchy
===============
G{classtree: CustomShell CustomShell._CustomCommand}

@author: U{Benjamin Bengfort<mailto:bbengfort@tacnetsol.com>}
@organization: Tactical Network Solutions
@license: Open Source MIT License
@copyright: S{copy} 2010 Tactical Network Solutions

@requires: Python 2.6

@version: 1.0
@change: I{example.py 2 2010-11-29 13:48:00 -0400 bbengfort}
@since: 2010-11-29 17:20:39 -0400

@todo: Create other examples folder
"""
__docformat__ = 'epytext en'

###################################################################### 
## Imports 
######################################################################

from interactive import InteractiveCommand, InteractiveShell

###################################################################### 
## Customized Shell
######################################################################

class CustomShell(InteractiveShell):
    
    ###################################################################### 
    ## Custom commands (private) 
    ######################################################################
    
    class _CustomCommand(InteractiveCommand):
        
        def get_name(self):
            return 'custom'

        def get_options(self):
            return { 'opt': None,
                     'foo=': 'default',
                     'bar=': None}

        def handle_command(self, opts, args):

            for k,v in opts.items( ):
                print '%s => %s' % (str(k), str(v))
            
            print 'Args is: %s' % args
            
        def get_short_description(self):
            return 'I am a custom command!'

        def get_help_message(self):
            return """\
This custom command simply shows what the arguments do and don't do. It is very
similar to the TestCommand, because it is the same exact thing.

Options:
    --opt: A flag, set optionally
    --foo: A settable option, not required, has a default value.
    --bar: A settable option, required, no default value
        """
    
    ###################################################################### 
    ## Overriden shell methods
    ######################################################################
    
    def __init__(self):
        InteractiveShell.__init__(self, '\nEasily convert metrics!')
        
        customcmd = CustomShell._CustomCommand( )
        self.add_command(customcmd)

if  __name__ == '__main__':
    
    console_program = CustomShell( )
    console_program.run( )